<template>
  <div>
    <!-- 内容列表 -->
    <div class="table-container select-table">
      <el-table ref="multipleTable" row-key="sortNo" :data="dataList" header-cell-class-name="default-header"
        row-class-name="default-row" element-loading-text="加载中" style="width: 100%">
        <el-table-column label=" " prop="sortNo">
          <template slot-scope="scope">
            <span label="" style="display:none">{{scope.row.sortNo}}</span>
            <img :src="draggable" alt="">
              <!-- <span>{{scope.row}}</span> -->
          </template>
        </el-table-column>
<!--        <el-table-column label="序号" prop="sortNo">
          <template slot-scope="scope">
            <el-input-number class="set-s-p-input" style="width: 70px" :controls='false' :disabled="comSortNoDisabled"
              v-model="scope.row.sortNo" label="序号"></el-input-number>
          </template>
        </el-table-column> -->
        <el-table-column min-width="300px" label="商品" prop="title">
          <template slot-scope="scope">
            <div class="flex v-align-c cursor-p">
              <label><img :src="scope.row.imageUrl" class="picture-avatar"></label>
              <div class="font-14 pad-l-10">
                <div class="title-line-2 font-14"><span style="color: red;"
                    v-if="scope.row.errorMsg !== null && scope.row.errorMsg !== undefined">（{{scope.row.errorMsg}}）</span>{{scope.row.title}}
                </div>
                <template v-if="scope.row.priceRange">
                  <div class="pad-t-6">
                    <span class="category-style" style="color: red;">{{scope.row.priceRange}}</span>
<!--                    <el-tag class="mar-l-10" size="mini" v-if="scope.row.skuFlag === 1" effect="light">多规格</el-tag> -->
                  </div>
                </template>
              </div>
            </div>
          </template>
        </el-table-column>
<!--        <el-table-column min-width="120px" label="零售价" prop="sellPrice" align="center">
          <template slot-scope="scope">
            ￥{{scope.row.sellPrice}}
          </template>
        </el-table-column> -->
        <el-table-column min-width="130px" label="活动库存" prop="preSaleStock" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.preSaleStock" style="width: 130px;" maxlength="5" :disabled="comPreSaleStockDisabled"
              @keyup.native="scope.row.preSaleStock = checkInputNum(scope.row.preSaleStock)">
              <template slot="append">件</template>
            </el-input>
          </template>
        </el-table-column>
        <el-table-column min-width="130px" label="单笔限购" prop="limitNum" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.limitNum" style="width: 130px;" maxlength="5" :disabled="disabledBoolean"
              @keyup.native="scope.row.limitNum = checkInputNum(scope.row.limitNum)">
              <template slot="append">件</template>
            </el-input>
          </template>
        </el-table-column>
        <el-table-column min-width="180px" label="团购价/佣金设置" prop="skuFlag" align="center">
          <template slot-scope="scope">
            <div style="float: left;">
              <el-input v-if="scope.row.skuFlag !== 1" placeholder="团购价" style="width: 70px"
                v-model="scope.row.promotionPrice" @change="changePrice(scope.row)" :disabled="disabledBoolean" />
              <el-button v-else type="primary" plain size="small" @click="setActivePrice(scope.row)"
                style="width: 70px">{{disabledBoolean?'查看':'设置'}}</el-button>
              <template v-if="getPerfectInfo(scope.row) == '1'">
                <br><a style="color: #00b200">已完善</a>
              </template>
              <template v-else>
                <br><a>待完善</a>
              </template>
            </div>
            <div style="float: right;">
              <el-input placeholder="团长佣金" class="set-s-p-input" style="width: 110px"
                v-model="scope.row.commanderCommissionRate"
                @keyup.native="scope.row.commanderCommissionRate = checkInputNum(scope.row.commanderCommissionRate)"
                :disabled="disabledBoolean">
                <template slot="append">%</template>
              </el-input>
            </div>
          </template>
        </el-table-column>
        <el-table-column min-width="100px" label="操作" align="center" fixed="right" v-if="!disabledBoolean">
          <template slot-scope="scope">
            <el-button type="text" class="del-t-btn" @click="deleteItem(scope.row)" :disabled="disabledBoolean">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>

</template>
<script>
import draggable from '@/assets/images/draggable.png'
import Sortable from 'sortablejs'
export default {
  props: {
    disabledBoolean: {
      type: Boolean,
      default: false
    },
    promotionStatus: {
      type: String,
      default: '-1'
    }
  },
  data() {
    return {
      dataList: [],
      draggable
    }
  },
  computed: {
    comSortNoDisabled() {
      if (this.promotionStatus !== '2') {
        return this.disabledBoolean
      }
      return false
    },
    comPreSaleStockDisabled() {
      if (this.promotionStatus !== '2') {
        return this.disabledBoolean
      }
      return false
    }
  },
  watch: {
    promotionStatus: {
      immediate: true,
      handler() {
        if (this.promotionStatus === '0' || this.promotionStatus === '1' || this.promotionStatus === '3' || this.promotionStatus === '2') {
          // 阻止默认行为
          document.body.ondrop = function(event) {
            event.preventDefault()
            event.stopPropagation()
          }
          this.rowDrop()
        }
      }
    }
  },
  mounted() {
    const that = this
    this.$bus.$on('promotion-sku-price-save', function(goodsInfo) {
      const goodsId = goodsInfo.goodsId
      const dataList = that.dataList
      dataList.forEach(obj => {
        if (obj.goodsId === goodsId) {
          obj.promotionGoodsSkuList = goodsInfo.promotionGoodsSkuList
          const promotionPrice = Math.min.apply(Math, goodsInfo.promotionGoodsSkuList.map(
            function(o) {
              return o.promotionPrice
            }))
          obj.promotionPrice = promotionPrice
        }
      })
      this.dataList = dataList
      that.updateData(dataList)
    })
    // 更新监听
    this.$bus.$on('promotion-update', function(data) {
      that.dataList = data.promotionGoodsList
      that.updateData(data.promotionGoodsList)
    })
  },
  filters: {},
  methods: {
    // 行拖拽
    rowDrop() {
      const _this = this
      this.$nextTick(() => {
        const tbody = document.querySelector('.el-table__body-wrapper tbody')
        Sortable.create(tbody, {
          onEnd({ newIndex, oldIndex }) {
            const currRow = _this.dataList.splice(oldIndex, 1)[0]
            _this.dataList.splice(newIndex, 0, currRow)
          }
        })
      })
    },
    changePrice(row) {
      this.$bus.$emit('promotion-item-phone', row.goodsId)
    },
    setActivePrice(row) {
      this.$bus.$emit('promotion-sku-price-open', row)
    },
    deleteItem(item) {
      // 删除商品
      const dataList = this.dataList
      dataList.splice(dataList.findIndex(obj => item.goodsId === obj.goodsId), 1)
      this.dataList = dataList
      // this.listData()
      this.updateData(dataList)
    },
    listData() {
      const dataList = this.dataList
      // dataList.sort((a, b) => {
      //   return b.sortNo - a.sortNo
      // })
      // for (let i = 0; i < dataList.length; i++) {
      //   if (!dataList[i].sortNo) {
      //     dataList[i].sortNo = 1
      //   }
      // }
      this.dataList = dataList
    },
    changeData() {
      const dataList = this.dataList
      this.updateData(dataList)
    },
    getPerfectInfo(row) {
      const skuFlag = row.skuFlag
      let ret = 1
      if (skuFlag === 1) {
        const skuList = row.promotionGoodsSkuList
        if (skuList) {
          skuList.forEach(e => {
            const promotionPrice = e.promotionPrice
            if (!this.$utils.isExist(promotionPrice) || promotionPrice === 0 || promotionPrice ===
                '0' || promotionPrice === '0.0' || promotionPrice === '0.00') {
              ret = 0
            }
          })
        }
      } else {
        const promotionPrice = row.promotionPrice
        if (promotionPrice === null || promotionPrice * 1 === 0) {
          ret = 0
        }
      }
      return ret
    },
    // 更新数据(其他组件勾选有使用)
    updateData(dataList) {
      this.dataList = dataList
      // this.listData()
      const data = this.dataList
      this.$bus.$emit('promotion-phone', data)
    }
  }
}
</script>
<style lang="less" scoped>
  .table-container {
    min-height: 250px;
  }

  .picture-avatar {
    border: unset;
  }

  // 页面总体布局
  .active-main {
    background: #f8f8f8;

    &:after {
      content: '';
      display: block;
      clear: both;
      font-size: 0;
    }

    .active-l {
      .main-content {
        padding-right: 4px;
        padding-left: 2px;
        height: 570px;
        overflow-x: hidden;
        overflow-y: auto;

        &::-webkit-scrollbar {
          width: 4px;
        }

        &::-webkit-scrollbar-thumb {
          border-radius: 10px;
          box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
          -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
          background: rgba(0, 0, 0, 0.2);
        }

        &::-webkit-scrollbar-track {
          box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
          -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
          border-radius: 0;
          background: rgba(0, 0, 0, 0.1);
        }

        h2 {
          margin: 0;
          font-size: 18px;
        }

        ul {
          padding: 0;
          margin: 0;

          &:after {
            content: '';
            display: block;
            clear: both;
            font-size: 0;
          }

          &.show-type-1 {
            li {
              .gooods-pic {
                img {
                  width: 100%;
                  height: 149px;
                  border-top-left-radius: 4px;
                  border-top-right-radius: 4px;
                }
              }
            }
          }

          &.show-type-2 {
            li {
              width: 48%;
              float: left;

              &:nth-child(odd) {
                margin-right: 4%;
              }

              .gooods-pic {
                img {
                  width: 100%;
                  height: 133px;
                  border-top-left-radius: 4px;
                  border-top-right-radius: 4px;
                }
              }

              .buy-now-type {
                display: none;
              }

              .price {
                label {
                  display: none;
                }
              }
            }
          }

          &.show-type-3 {
            li {
              display: -webkit-box;
              display: -webkit-flex;
              display: flex;
              flex-flow: row;
              align-items: center;
              padding: 0 5px 0 8px;

              .pic-box {
                margin-right: 6px;

                .gooods-pic {
                  img {
                    width: 82px;
                    height: 82px;
                  }
                }
              }

              .detail-box {
                -webkit-box-flex: 1;
                -webkit-flex: 1;
                flex: 1;
              }

              .tag {
                span {
                  padding: 1px 5px !important;
                }

                label {
                  padding: 1px 5px !important;
                }
              }

              .price {
                label {
                  display: none;
                }
              }

              .stock {
                display: none;
              }
            }
          }

          &.show-type-4 {
            li {
              display: -webkit-box;
              display: -webkit-flex;
              display: flex;
              flex-flow: row;
              align-items: center;
              padding: 8px 5px 8px 8px;

              &:nth-child(even) {
                .pic-box {
                  &.even {
                    display: none;
                  }

                  &.odd {
                    display: block;
                  }
                }
              }

              &:nth-child(odd) {
                .pic-box {
                  &.even {
                    display: block;
                  }

                  &.odd {
                    display: none;
                  }
                }
              }

              .pic-box {
                margin-right: 6px;

                .gooods-pic {
                  img {
                    width: 122px;
                    height: 122px;
                  }
                }
              }

              .detail-box {
                -webkit-box-flex: 1;
                -webkit-flex: 1;
                flex: 1;
                padding: 0 6px !important;
              }

              .desc {
                margin-top: -6px;
              }

              .tag {
                margin-top: -1px;
                margin-bottom: 0px;

                span {
                  padding: 1px 5px !important;
                }

                label {
                  padding: 1px 5px !important;
                }
              }

              .price {
                .flex-item {
                  >label {
                    display: -webkit-box;
                    display: -webkit-flex;
                    display: flex;
                    margin-top: -8px;
                  }
                }
              }

              .stock {
                display: none;
              }

              .info-bar {
                .buy-now-type {
                  display: none !important;
                }
              }

              .last-item {
                display: block !important;
                margin-top: 5px;
              }
            }
          }

          li {
            list-style-type: none;
            border-radius: 4px;
            box-shadow: 0 0 3px rgba(0, 0, 0, 0.3);
            margin-bottom: 15px;

            &:last-child {
              margin-bottom: 0;
            }

            .pic-box {
              &.odd {
                display: none;
              }
            }

            .gooods-pic {
              img {
                display: block;
              }
            }

            .desc {
              height: 36px;
              line-height: 1.5;
              overflow: hidden;
              text-overflow: ellipsis;
              display: -webkit-box;
              -webkit-line-clamp: 2;
              -webkit-box-orient: vertical;
              word-break: break-word;
            }

            .last-item {
              display: none;
            }

            .info-bar {
              .tag {
                line-height: 1.3;

                span {
                  background: #fed807;
                  color: #333;
                  padding: 2px 5px;
                }

                label {
                  background: #272727;
                  color: #fff;
                  padding: 2px 5px;
                  min-width: 60px;
                  box-sizing: border-box;

                  i {
                    color: #ffc81b;
                    font-style: normal;
                  }
                }
              }

              .price {
                span {
                  color: #FF4949;
                }

                label {
                  text-decoration: line-through;
                }
              }

              .stock {
                color: #6e6e6e;
              }

              label {
                font-weight: normal;
              }
            }
          }
        }

        .picture-default {
          background: #fe8096;
          display: flex;
          justify-content: center;
          align-items: center;
        }
      }
    }

    .active-r {
      border: 1px solid #ddd;
      border-radius: 10px;
      background: #fff;
      padding: 20px;
      min-height: 609px;

      h2 {
        font-size: 18px;
        margin: 0;
      }

      .tips {
        color: #409eff;
      }

      .select-table {
        min-height: auto !important;
      }
    }
  }

  .category-style {
    color: #6290ff;
    font-size: 13px;
  }

  .title-line-2 {
    height: 40px;
    line-height: 1.4;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    word-break: break-word;
  }

  // 商品弹窗样式
  .goods-layer {
    h3 {
      margin: -10px 0 0 0;
      color: #303133;
      font-size: 18px;
      text-align: center;
    }

    .dialog-footer {
      position: absolute;
      left: 0;
      bottom: 20px;
      width: 100%;
    }

    .table-container {
      height: 500px;
      overflow-x: hidden;
      overflow-y: auto;
    }
  }

  // 左侧手机模型样式
  .diy-bg-main {
    .diy-phone {
      float: left;
      width: 340px;
      border: 1px solid #ddd;
      border-radius: 10px;
      background: #fff;
      padding: 10px;
      overflow: hidden;

      &.none-border {
        border: none;
        background: none;
        padding: 0px 10px;
        border-radius: 0;
      }
    }

    .diy-phone .phone-head {
      height: 44px;
      background: #fff;
      position: relative;
    }

    .diy-phone .phone-head:before {
      content: "";
      position: absolute;
      top: 15px;
      left: 112px;
      height: 8px;
      width: 8px;
      background: #ddd;
      border-radius: 8px;
    }

    .diy-phone .phone-head:after {
      content: "";
      position: absolute;
      top: 15px;
      left: 128px;
      height: 8px;
      width: 50px;
      background: #ddd;
      border-radius: 8px;
    }

    .diy-phone .phone-title {
      height: 17px;
      /*background: url(../../../../../assets/images/wx-top-2.png) 0 0 no-repeat;*/
      background-size: 100% auto;
      padding: 0 50px;
      font-size: 15px;
      color: #333333;
      text-align: center;
      line-height: 68px;
      cursor: default;
      overflow: hidden;
      display: block;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
  }
</style>
